<?php
$themename = "iGoogler";
$shortname = "iGoogler_";
global $options;
$options = array (
array("id" => "excerpt_first_p","title"=>"自动选取第一段作为摘要","type"=>"checkbox", "std" => "true", "description" => "选择是则自动摘取第一段作为摘要，否则摘取全文的字数"),
array("id" => "excerpt_length","title"=>"摘要最大长度","type"=>"text", "std" => "200", "description"=>"最大的摘要长度"),
array("id" => "google-plus-one","title"=>"是否启用Google+1","type"=>"checkbox", "std"=>"true", "description"=>"启用Google+1有助于增加网站流量，详见<a href='http://www.google.com/webmasters/+1/button/'>Google +1</a>"),
array("id" => "google_custom","title"=>"谷歌自定义搜索ID","type"=>"text", "std"=> "", "description"=>"请填写例如“009074447596699951990:e_y5ug***-#”，<a href='http://www.google.com/cse/?hl=zh-CN'>点击这儿</a>注册Google自定义搜索引擎"),
array("id" => "single_copyright","title"=>"是否显示版权提示","type"=>"checkbox","std" => "true","description"=>"选中后，文章页面会显示版权提示"),
array("id" => "copyright_content","title"=>"版权提示内容","type"=>"textarea","std" => "","description"=>"您可以留空该选项，此时显示默认效果"),
array("id" => "site_stat","title"=>"网站统计代码","type"=>"textarea","std" => "","description"=>"如果您需要，可以在这个位置添加网站统计代码，例如Google Analytics或百度统计"),
array("id" => "h3","title"=>"广告位设置","type"=>"h3"),
array("id" => "bottom_ad", "title" => "文章底部广告位","type"=>"textarea","std"=>"","description"=>"文章底部的广告位，建议添加336*280或300*250的广告，不需要请留空"),
array("id" => "respond_ad", "title" => "评论右侧广告位","type"=>"textarea","std"=>"","description"=>"评论框右侧的广告位，建议添加300*250的广告，不需要请留空"),
);

function igoogler_admin_panel() {
	global $themename;
	global $shortname;
	global $options;
	if ( $_GET['page'] == basename(__FILE__) ) {
		if ( 'save' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				update_option( $shortname.$value['id'], $_REQUEST[ $value['id'] ] ); }
				foreach ($options as $value) {
					if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
					header("Location: themes.php?page=func_panel.php&saved=true");
					die;
		} else if( 'reset' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				update_option( $shortname.$value['id'], $value['std'] );
			}
			header("Location: themes.php?page=func_panel.php&reset=true");
			die;
		}
	}
	add_theme_page($themename." Options", 'iGoogler设置', 'edit_themes', basename(__FILE__), 'igoogler_admin');
}
function igoogler_admin() {
	global $themename, $shortname, $options;
	if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' 设置已保存。</strong></p></div>';
	if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' 设置已重置。</strong></p></div>';
	?>

<div class="wrap">
  <div id="icon-options-general" class="icon32"><br>
  </div>
  <h2>iGoogler 主题设置</h2>
  <form method="post">
    <input type="hidden" name="action" value="save" />
    <div id="message" class="updated fade">
      <p>为了提高主题速度，已关闭自动检查更新。请定期访问<a href="http://zyj.me/igoogler">iGoogler</a>了解详情，并访问<a href="http://zyj.me/wordpress-theme/igoogler">Google Code</a>下载更新。</p>
    </div>
    <h3>常规设置</h3>
    <table class="form-table">
      <tbody>
        <?php foreach ($options as $value):?>
        <tr>
          <th scope="row"> <?php if($value['type'] == "h3"): ?>
            <h3><?php echo $value['title'];?></h3>
            <?php else:?>
            <?php echo $value['title']?>
            <?php endif;?></th>
          <td><?php if($value['type'] == "h3"): ?>
            <?php elseif ($value['type'] == "checkbox"):?>
            <input type="<?php echo $value['type']?>" name="<?php echo $value['id'];?>" id="<?php echo $value['id']?>" <?php if(get_option($shortname.$value['id'],$value['std'])==true)echo 'checked="true"';?> />
            <?php elseif ($value['type'] == "textarea"):?>
            <textarea name="<?php echo $value['id'];?>" id="<?php echo $value['id'];?>" class="large-text code"><?php echo stripslashes(get_option($shortname.$value['id'],$value['std']));?></textarea>
            <br />
            <?php else:?>
            <input type="<?php echo $value['type']?>" name="<?php echo $value['id'];?>" id="<?php echo $value['id']?>" value="<?php echo get_option($shortname.$value['id'],$value['std']);?>" />
            <?php endif;?>
            <span class="description"><?php echo $value['description']?></span></td>
        </tr>
        <?php endforeach;?>
      </tbody>
    </table>
    <h3>如果您在使用主题中遇到问题，请 <a href="mailto:willings.zhang@gmail.com"><strong>点击这儿</strong></a>反馈Bug</h3>
    <p class="submit">
      <input type="submit" class="button-primary"
	value="保存更改" />
    </p>
  </form>
</div>
<?php
}
add_action('admin_menu', 'igoogler_admin_panel');
?>
